#pragma once

#include <iostream>
#include <memory>
#include "Protocol.hpp"

// 业务
namespace CalCulate
{

    enum ErrCode
    {
        Success = 0,
        DivZreoErr,
        ModZreoErr,
        Unknown
    };

    class Calculate
    {
    public:
        Calculate()
        {
        }

        shared_ptr<Protocol::Reponse> Cal(shared_ptr<Protocol::Request> req)
        {
            shared_ptr<Protocol::Reponse> resp = factory.BuildReponse();
            resp->SetCode(Success); // 将状态
            switch (req->GetOper())
            {
            case '+':
                resp->SetResult(req->GetX() + req->GetY());
                break;
            case '-':
                resp->SetResult(req->GetX() - req->GetY());
                break;
            case '*':
                resp->SetResult(req->GetX() * req->GetY());
                break;
            case '/':
            {
                if (req->GetY() == 0)
                {
                    resp->SetCode(ErrCode::DivZreoErr);
                }
                else
                {
                    resp->SetResult(req->GetX() / req->GetY());
                }
                break;
            }

            case '%':
            {
                if (req->GetY() == 0)
                {
                    resp->SetCode(ErrCode::ModZreoErr);
                }
                else
                {
                    resp->SetResult(req->GetX() % req->GetY());
                }
                break;
            }
            default:
                resp->SetCode(ErrCode::Unknown);
                break;
            }

            return resp;
        }

        ~Calculate()
        {
        }

    private:
        Protocol::Factory factory;
    };
}
